package com.jiajia.study.nio.chapter01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestNio01{
    public static void main(String[] args) throws IOException {
        // 创建通道
        String path = "C:\\IDEAWorkPlace\\2025_lab\\sunsuns_netty\\files\\test1.txt";
        FileChannel channel = new FileInputStream(path).getChannel();

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        //
        while(true){
            int read = channel.read(byteBuffer);
            if(read == -1){
                break;
            }
            // 让程序读取 buffer 内容，后续的操作，设置 buffer 为读模式
            byteBuffer.flip();

            // 循环读取缓冲区的内容
            while(byteBuffer.hasRemaining()){
                byte b = byteBuffer.get();
                System.out.println("the result is : " + (char)b);
            }

            // 切换成写模式
            byteBuffer.clear();
        }


    }
}
